package com.biancheng.biancheng.biancheng2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class 进程排序 {

    public static void main(String [] args){
        Scanner sc=new Scanner(System.in);
        String s1=sc.nextLine();
        String s2=sc.nextLine();
        String s3=sc.nextLine();
        int n=Integer.parseInt(s1.split(" ")[0]);
        int k=Integer.parseInt(s1.split(" ")[1]);
        int [] pid=new int[n];
        int [] ppid=new int[n];
        for(int i=0;i<n;i++){
            pid[i]=Integer.parseInt(s2.split(" ")[i]);
            ppid[i]=Integer.parseInt(s3.split(" ")[i]);
        }
        List<Integer> plist=new ArrayList();
        plist.add(k);
        int h=0;
        do{
            for(int i=0;i<n;i++){
                if(ppid[i]==plist.get(h)){
                    plist.add(pid[i]);
                }
            }
            h++;
        }while(h<plist.size());
        Collections.sort(plist);
        StringBuilder jg=new StringBuilder();
        for(int i:plist){
            jg.append(i+" ");
        }
        System.out.print(jg.toString().trim());
    }

}
